package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Override
    public RoomDetailVo getRoom(Long id) {
        List<GraphVo> graphVos = graphInfoMapper.queryVo(id, ItemType.ROOM);
        List<FacilityInfo> facilityInfos = facilityInfoMapper.queryInfo(id, ItemType.ROOM);
        List<LabelInfo> labelInfos = labelInfoMapper.queryInfo(id, ItemType.ROOM);
        ApartmentInfo apartmentInfo=apartmentInfoMapper.queryApartmentByRoomId(id);
        List<PaymentType> paymentTypes=paymentTypeMapper.queryInfo(id);
        List<LeaseTerm> leaseTerms=leaseTermMapper.queryInfo(id);
        List<AttrValueVo> attrValues=attrValueMapper.queryVo(id);
        RoomInfo roomInfo = getById(id);
        RoomDetailVo roomDetailVo=new RoomDetailVo();
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setFacilityInfoList(facilityInfos);
        roomDetailVo.setLabelInfoList(labelInfos);
        roomDetailVo.setGraphVoList(graphVos);
        roomDetailVo.setLeaseTermList(leaseTerms);
        roomDetailVo.setPaymentTypeList(paymentTypes);
        roomDetailVo.setAttrValueVoList(attrValues);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        LambdaQueryWrapper<LeaseAgreement> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(LeaseAgreement::getRoomId,id).in(LeaseAgreement::getStatus,2,7);
        long count = leaseAgreementService.count(wrapper);
        if (count>0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        removeById(id);
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueWrapper=new LambdaQueryWrapper<>();
        roomAttrValueWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueWrapper);
        LambdaQueryWrapper<RoomFacility> roomFacilityWrapper=new LambdaQueryWrapper<>();
        roomFacilityWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityWrapper);
        LambdaQueryWrapper<RoomLabel> roomLabelWrapper=new LambdaQueryWrapper<>();
        roomLabelWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelWrapper);
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper=new LambdaQueryWrapper<>();
        roomLeaseTermWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermWrapper);
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper=new LambdaQueryWrapper<>();
        roomPaymentTypeWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeWrapper);
        LambdaQueryWrapper<GraphInfo> graphInfoWrapper=new LambdaQueryWrapper<>();
        graphInfoWrapper.eq(GraphInfo::getItemId,id);
        graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(graphInfoWrapper);
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        boolean isUpdate=roomSubmitVo.getId()!=null && roomSubmitVo.getId()!=0;
        saveOrUpdate(roomSubmitVo);
        Long id = roomSubmitVo.getId();
        if (isUpdate){
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueWrapper=new LambdaQueryWrapper<>();
            roomAttrValueWrapper.eq(RoomAttrValue::getRoomId,id);
            roomAttrValueService.remove(roomAttrValueWrapper);
            LambdaQueryWrapper<RoomFacility> roomFacilityWrapper=new LambdaQueryWrapper<>();
            roomFacilityWrapper.eq(RoomFacility::getRoomId,id);
            roomFacilityService.remove(roomFacilityWrapper);
            LambdaQueryWrapper<RoomLabel> roomLabelWrapper=new LambdaQueryWrapper<>();
            roomLabelWrapper.eq(RoomLabel::getRoomId,id);
            roomLabelService.remove(roomLabelWrapper);
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper=new LambdaQueryWrapper<>();
            roomLeaseTermWrapper.eq(RoomLeaseTerm::getRoomId,id);
            roomLeaseTermService.remove(roomLeaseTermWrapper);
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper=new LambdaQueryWrapper<>();
            roomPaymentTypeWrapper.eq(RoomPaymentType::getRoomId,id);
            roomPaymentTypeService.remove(roomPaymentTypeWrapper);
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper=new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemId,id);
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(graphInfoWrapper);
        }
        if (roomSubmitVo.getFacilityInfoIds()!=null&&!roomSubmitVo.getFacilityInfoIds().isEmpty()){
            List<Long> infoIds = roomSubmitVo.getFacilityInfoIds();
            List<RoomFacility> facilities=new ArrayList<>(infoIds.size());
            for (Long infoId : infoIds) {
                facilities.add(RoomFacility.builder().roomId(id).facilityId(infoId).build());
            }
            roomFacilityService.saveBatch(facilities);
        }
        if (roomSubmitVo.getGraphVoList()!=null&&!roomSubmitVo.getGraphVoList().isEmpty()){
            List<GraphVo> graphVos = roomSubmitVo.getGraphVoList();
            List<GraphInfo> graphInfos=new ArrayList<>(graphVos.size());
            for (GraphVo vo : graphVos) {
                GraphInfo graphInfo=new GraphInfo();
                graphInfo.setName(vo.getName());
                graphInfo.setUrl(vo.getUrl());
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfos.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfos);
        }
        if (roomSubmitVo.getLabelInfoIds()!=null&&!roomSubmitVo.getLabelInfoIds().isEmpty()){
            List<Long> infoIds = roomSubmitVo.getLabelInfoIds();
            List<RoomLabel> labels=new ArrayList<>(infoIds.size());
            for (Long infoId : infoIds) {
                labels.add(RoomLabel.builder().roomId(id).labelId(infoId).build());
            }
            roomLabelService.saveBatch(labels);
        }
        if (roomSubmitVo.getLeaseTermIds()!=null&&!roomSubmitVo.getLeaseTermIds().isEmpty()){
            List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
            List<RoomLeaseTerm> leaseTerms=new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                leaseTerms.add(RoomLeaseTerm.builder().roomId(id).leaseTermId(leaseTermId).build());
            }
            roomLeaseTermService.saveBatch(leaseTerms);
        }
        if (roomSubmitVo.getPaymentTypeIds()!=null&&!roomSubmitVo.getPaymentTypeIds().isEmpty()){
            List<Long> infoIds = roomSubmitVo.getPaymentTypeIds();
            List<RoomPaymentType> paymentTypes=new ArrayList<>(infoIds.size());
            for (Long infoId : infoIds) {
                paymentTypes.add(RoomPaymentType.builder().roomId(id).paymentTypeId(infoId).build());
            }
            roomPaymentTypeService.saveBatch(paymentTypes);
        }
        if (roomSubmitVo.getAttrValueIds()!=null&&!roomSubmitVo.getAttrValueIds().isEmpty()){
            List<Long> infoIds = roomSubmitVo.getAttrValueIds();
            List<RoomAttrValue> attrValues=new ArrayList<>(infoIds.size());
            for (Long infoId : infoIds) {
                attrValues.add(RoomAttrValue.builder().roomId(id).attrValueId(infoId).build());
            }
            roomAttrValueService.saveBatch(attrValues);
        }

    }

    @Override
    public void listRooms(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        page=roomInfoMapper.listRooms(page,queryVo);
    }
}




